package tasknow.commandlogic;

import java.io.IOException;
import java.util.Vector;

import tasknow.parser.Parser;
import tasknow.utils.Constants;
import tasknow.utils.Task;

// @author A0096638M

public class Logic {

	public static String processCommand(String input) throws IOException {
		String feedback = parseCommand(input);

		CommandLogic.updateTodayTasksVector();
		CommandLogic.updateNextWeekTasksVector();
		CommandLogic.saveFile();

		return feedback;
	}

	public static void getTodayNotification() {
		CommandLogic.notifyNumberOfTasksToday(CommandLogic.todayTasks);
	}
	
	public static Vector<Task> getTodayTasksVector() {
		return CommandLogic.todayTasks;
	}

	public static Vector<Task> getNextWeekTasksVector() {
		return CommandLogic.nextWeekTasks;
	}

	public static Vector<Task> getCurrentTaskVector() {
		return CommandLogic.taskVector;
	}

	public static Vector<Task> getSearchVector() {
		return CommandLogic.searchVector;
	}

	public static Vector<Task> getArchivedVector() {
		return CommandLogic.archivedTaskVector;
	}

	public static void initiateStorage() {
		CommandLogic.initiateStorage();		
	}

	public static String getTodayTasks() {
		return CommandLogic.getTodayTasks();
	}

	public static String getNextWeekTasks() {
		return CommandLogic.getNextWeekTasks();
	}

	public static String parseCommand(String input) {
		String args[] = splitInputs(input);

		if (args.length >= Constants.LENGTH_COMMAND_AND_INPUT) {
			Parser.transformToAcceptableArgs(args);
		}

		Constants.COMMANDS command = Parser.parseCommandType(args[Constants.INDEX_COMMAND]);

		return CommandLogic.executeCommand(args, command);
	}

	private static String[] splitInputs(String input) {
		String[] args = new String[Constants.LENGTH_COMMAND_AND_INPUT];

		if (input.startsWith(Constants.SHORTCUT_PLUS)) {
			int length = input.length();
			args[Constants.INDEX_COMMAND] = Constants.STRING_ADD;
			args[Constants.INDEX_INPUT] = input.substring(Constants.FIRST,
					length);
		} else if (input.startsWith(Constants.SHORTCUT_MINUS)) {
			int length = input.length();
			args[Constants.INDEX_COMMAND] = Constants.STRING_DELETE;
			args[Constants.INDEX_INPUT] = input.substring(Constants.FIRST,
					length);

		} else if (isSingleCommand(input)) {
			args[Constants.INDEX_COMMAND] = input;
			args[Constants.INDEX_INPUT] = Constants.EMPTY_STRING;
		} else {
			args = input.split(Constants.BLANK_SPACE,
					Constants.LENGTH_COMMAND_AND_INPUT);
		}

		return args;
	}

	private static boolean isSingleCommand(String input) {
		return !input.contains(Constants.BLANK_SPACE);
	}

}
